<?php 
	/**
	* FILE: ProcessAuthToRelease.php
	* PATH: /web_root/actions/
	* NOTES:
	*
	*/

	/**
	* initialize includes
	*/
	include('../includes/db_connect.php'); 
	include('../includes/phpheader.php');

	/**
	* handle form vars
	*/
	
	/**
	* form id
	*/
	$id = $_POST["id"];
	
	/**
	* loggedin id
	*/
	$userId = $_SESSION['user_id'];

	/**
	* list of agency options -- should be in a loop
	*/
	$agency1 = $_POST["agency1"];
	$agency2 = $_POST["agency2"];
	$agency3 = $_POST["agency3"];
	$agency4 = $_POST["agency4"];
	$agency5 = $_POST["agency5"];
	$person1 = $_POST["person1"];
	$person2 = $_POST["person2"];
	$person3 = $_POST["person3"];
	$person4 = $_POST["person4"];
	$person5 = $_POST["person5"];
	
	/**
	*consumer form data
	*/
	$consumerSig 				= $_POST["consumerSignature"];
	$consumerSigDateMonth 	= $_POST["consumerSigDateMonth"];
	$consumerSigDateDay 		= $_POST["consumerSigDateDay"];
	$consumerSigDateYear 	= $_POST["consumerSigDateYear"];
	$consumerSigDate 			= $consumerSigDateYear."-".$consumerSigDateMonth."-".$consumerSigDateDay;

	/**
	*guardian for data
	*/
	$guardianSig 				= $_POST["guardianSignature"];
	$guardianSigDateMonth 	= $_POST["guardianSigDateMonth"];
	$guardianSigDateDay 		= $_POST["guardianSigDateDay"];
	$guardianSigDateYear 	= $_POST["guardianSigDateYear"];
	$guardianSigDate 			= $guardianSigDateYear."-".$guardianSigDateMonth."-".$guardianSigDateDay;

	/**
	*coordinator sig
	*/
	$employeeSig = $_POST["employeeSignature"];	

	/**
	* find the current ATR instance in the db
	*/
	$query = "SELECT id 
				  FROM authToRelease 
				  WHERE consumer_id = ".$id;

				  
	if(mysql_fetch_array(mysql_query($query))) {
	
		/**
		* we found the record, so lets just update it
		*/
		$query = "update authToRelease set agency1 = '".$agency1."', agency2 = '".$agency2."', agency3 = '".$agency3."', agency4 = '".$agency4.
	"', agency5 = '".$agency5."', person1 = '".$person1."', person2 = '".$person2."', person3 = '".$person3."', person4 = '".$person4.
	"', person5 = '".$person5."', consumer_signature = '".$consumerSig."', consumer_signature_date = '".$consumerSigDate.
	"', guardian_signature = '".$guardianSig."', guardian_signature_date = '".$guardianSigDate."', employee_signature = '".
	$employeeSig.
	"' where consumer_id = ".$id;
	} else {
	
		/**
		*this is a new ATR record, so let's create it
		*/
		$query = "insert into authToRelease (consumer_id, agency1, agency2, agency3, agency4, agency5, person1, person2, person3,
	person4, person5, consumer_signature, consumer_signature_date, guardian_signature, guardian_signature_date, employee_signature, entered_by) 
		VALUES 
	(".$id.", '".$agency1."', '".$agency2."', '".$agency3."', '".$agency4."', '".$agency5."', '".
		$person1."', '".$person2."', '".$person3."', '".$person4."', '".$person5."', '".$consumerSig."', '".$consumerSigDate."', '".
		$guardianSig."', '".$guardianSigDate."', '".$employeeSig."', ".$userId.");";
	}

	/**
	*
	*/
	if(mysql_query($query)) {
		header("Location:../admin.php?action=cieedit&id=".$id."&Status=Authorization To Release Form Submitted Successfully");
		exit();
		echo "Success <br/>".$query;
	} else {
		header("Location:../admin.php?action=cieedit&id=".$id."&Error=Unable to process Authorization To Release Form due to system error.  Please contact system administrator.");
		exit();
		echo "Failure <br/>".$query;
	}
?>